package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity3 extends AppCompatActivity implements Runnable{
    TextView nb;
    EditText et;
    Button dl,er,wo;
    private static final String TAG = "MainActivity3";
    double d=0.1383;
    double e=0.1283;
    double w=0.054;
    Handler handler=new Handler();



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nb = findViewById(R.id.nb);
        et = findViewById(R.id.et);
        dl=(Button)findViewById(R.id.dl);
        er=(Button)findViewById(R.id.er);
        wo=(Button)findViewById(R.id.wo);


        handler=new Handler(Looper.myLooper()){
            public void handleMessage(@NonNull Message msg){
                if(msg.what==6){
                    Log.i(TAG,"handleMessage:内容："+msg.obj);
                    Toast.makeText(MainActivity3.this,"收到"+msg.obj,Toast.LENGTH_SHORT).show();

                }
                super.handleMessage(msg);
            }


        };
        Thread t = new Thread(this);
        t.start();
        Log.i(TAG,"OnCreate:t.start()....");








    }


    public void Clickdl(View v){
        Log.i(TAG,"myClick:11111");
        Log.i(TAG,"myClick");
        String m=et.getText().toString();
        double money = Double.parseDouble(m);

        double dr=d*money;
        nb.setText(""+dr);
    }

    public void Clicker(View v){
        Log.i(TAG,"myClick:11111");
        Log.i(TAG,"myClick");
        String m=et.getText().toString();
        double money = Double.parseDouble(m);

        double de=e*money;
        nb.setText(""+de);

    }
    public void Clickwo(View v){
        Log.i(TAG,"myClick:11111");
        Log.i(TAG,"myClick");

        String m=et.getText().toString();
        double money = Double.parseDouble(m);
        double dw=w*money;
        nb.setText(""+dw);

    }
    public void openConfig(View btn){
        Intent config = new Intent(this,com.example.myapplication.config.class);
        config.putExtra("dollerkey",d);
        config.putExtra("eurokey",e);
        config.putExtra("wonkey",w);
        startActivityForResult(config,6);

    }
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        if(requestCode==6 && resultCode==2){
            Bundle bdl2=data.getExtras();
            d=bdl2.getFloat("dollerkey2",0f);
            e=bdl2.getFloat("eurokey2",0f);
            w=bdl2.getFloat("wonkey2",0f);


        }
        super.onActivityResult(requestCode,resultCode,data);


    }
    public void run(){
        Log.i(TAG,"run:()....");
        try{
            Thread.sleep(3000);

        }catch (InterruptedException e){
            throw new RuntimeException(e);
        }
        URL url=null;
        try{
           Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
           Elements tables = doc.getElementsByTag("table");
           Element table2 = tables.get(1);
           Elements trs = table2.getElementsByTag("tr");
           for (Element tr : trs){
               Elements tds = tr.getElementsByTag("td");
               if(tds.size()>6){
                   String rname = tds.get(0).text();
                   String rateStr = tds.get(5).text();
                   Log.i(TAG,"run:"+rname+"==>"+rateStr);
                   if(rname.equals("欧元")){
                       Log.i(TAG,"run:欧元汇率="+rateStr);
                       float ee = Float.parseFloat(rateStr);
                       e=1/(ee/100);
                   }
                   if(rname.equals("美元")){
                       Log.i(TAG,"run:美元汇率="+rateStr);
                       float dd = Float.parseFloat(rateStr);
                       d=1/(dd/100);
                   }
                   if(rname.equals("韩国元")){
                       Log.i(TAG,"run:韩元汇率="+rateStr);
                       float ww = Float.parseFloat(rateStr);
                       w=1/(ww/100);
                   }
               }
           }




        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(6);
        msg.obj="Hello Android";
        handler.sendMessage(msg);
        Log.i(TAG,"run:msg已发送");

    }

    }


